/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import javax.servlet.jsp.tagext.TryCatchFinally;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import pojo.User;
import until.HibernateUtil;
import until.MySqlDataAccessHelper;

/**
 *
 * @author Administrator
 */
public class user_DAO {

    public static ArrayList<User> layDSCacUser() {
        ArrayList<User> ds = new ArrayList<User>();
        String sql = "select * from user";
        MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
        helper.open();
        ResultSet rs = helper.executeQuery(sql);
        try {
            while (rs.next()) {
                User nguoiDung = new User();
                nguoiDung.setTenDangNhap(rs.getString("TenDangNhap"));
                nguoiDung.setMatKhau(rs.getString("MatKhau"));
                ds.add(nguoiDung);
            }
        } catch (Exception e) {
        }
        helper.close();
        return ds;
    }
     public static User layThongTinUser(String userName){
         User nguoiDung= new User();
         String sql="select * from user where TenDangNhap=N'"+userName+"'";
         MySqlDataAccessHelper helper= new MySqlDataAccessHelper();
         helper.open();
         ResultSet rs = helper.executeQuery(sql);
         try {
             while(rs.next()){
                 nguoiDung.setId(rs.getInt("ID"));
                 nguoiDung.setTenDangNhap(rs.getString("TenDangNhap"));
                 nguoiDung.setMatKhau(rs.getString("MatKhau"));
                 nguoiDung.setHoTen(rs.getString("HoTen"));
                 nguoiDung.setNgaySinh(rs.getString("NgaySinh"));
                 nguoiDung.setGioiTinh(rs.getString("GioiTinh"));
                 nguoiDung.setEmail(rs.getString("Email"));
                 nguoiDung.setDiaChi(rs.getString("DiaChi"));
                 nguoiDung.setDienThoai(rs.getString("DienThoai"));
                 nguoiDung.setHinhDaiDien(rs.getString("HinhDaiDien"));
                 nguoiDung.setDaKichHoat(rs.getInt("DaKichHoat"));
                 nguoiDung.setLichSuViengTham(rs.getDate("LichSuViengTham"));
                 nguoiDung.setNgayDangKy(rs.getDate("NgayDangKy"));
                 nguoiDung.setIp(rs.getString("IP"));
             }
         } catch (Exception e) {
         }
         return nguoiDung;
     }
    public static ArrayList<User> laytoanbothongtinuser() {
        ArrayList<User> ds = new ArrayList<User>();
        Session Hsession = HibernateUtil.getSessionFactory().openSession();
        try {
            User nguoidung = null;
            nguoidung = (User) Hsession.get(User.class, Hsession);
            ds.add(nguoidung);
        } catch (Exception e) {
        }
        Hsession.close();
        return ds;

    }

    public static int KiemTraTrungLap(String ten) {
        int flag = 1;
        ArrayList<User> dskh = user_DAO.layDSCacUser();
        for (int i = 0; i < dskh.size(); i++) {
            User nguoiDung = dskh.get(i);
            String TenDangNhap = nguoiDung.getTenDangNhap();
            if (TenDangNhap.equals(ten)) {
                flag = -1;
            }
        }
        return flag;
    }
    public static int KiemTraTrungLap2(String tenNhap,String matKhauNhap) {
        int flag = 1;
        ArrayList<User> dskh = user_DAO.layDSCacUser();
        for (int i = 0; i < dskh.size(); i++) {
            User nguoiDung = dskh.get(i);
            String TenDangNhap = nguoiDung.getTenDangNhap();
            String matKhau=nguoiDung.getMatKhau();
            if (TenDangNhap.equals(tenNhap)&& matKhau.equals(matKhauNhap)) {
                return flag = -1;
            }
        }
        return flag;
    }

    public static int ThemUser(User nguoiDungMoi) {
        int maKhachHang = 0;
         int kqkt= KiemTraTrungLap(nguoiDungMoi.getTenDangNhap());
         if (kqkt==-1) {
             maKhachHang=-1;
         }else{
             try {
                     String sql = "insert into `user`(TenDangNhap,MatKhau,HoTen,NgaySinh,GioiTinh,Email,DiaChi,DienThoai,HinhDaiDien) values(N'"+nguoiDungMoi.getTenDangNhap()+"',N'"+nguoiDungMoi.getMatKhau()+"',N'"+nguoiDungMoi.getHoTen()+"',N'"+nguoiDungMoi.getNgaySinh()+"',N'"+nguoiDungMoi.getGioiTinh()+"',N'"+nguoiDungMoi.getEmail()+"',N'"+nguoiDungMoi.getDiaChi()+"',"+nguoiDungMoi.getDienThoai()+",N'"+nguoiDungMoi.getHinhDaiDien()+"')";
                     MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
                     helper.open();
                     int n= helper.executeUpdate(sql);
                     if(n!=1){
                         maKhachHang=-1;
                     }
                     helper.close();
                  } catch (Exception ex) {
                     System.out.println(ex.getMessage());
                  }
         }
         
        return maKhachHang;
    }
//
//    public static int ThemUserNhibernate(User nguoiDungMoi) {
//        int maKhachHang = 0;
////         int kqkt= KiemTraTrungLap(kh.getTenDangNhap());
////         if (kqkt==-1) {
////             maKhachHang=-1;
////         }else{
////             try {
////                     String sql = "insert into khachhang(TenDangNhap,MatKhau,HoVaTen,DiaChi,Email,DienThoai) values(N'"+kh.getTenDangNhap()+"',N'"+kh.getMatKhau()+"',N'"+kh.getHoVaTen()+"',N'"+kh.getDiaChi()+"','"+kh.geteMail()+"','"+kh.getDienThoai()+"')";
////                     MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
////                     helper.open();
////                     int n= helper.executeUpdate(sql);
////                     if(n!=1){
////                         maKhachHang=-1;
////                     }
////                     helper.close();
////                  } catch (Exception ex) {
////                     System.out.println(ex.getMessage());
////                  }
////         }
////         
//        return maKhachHang;
//    }
    // Tham Khao nHibernate
    // add mot acc
    //    Account acc = new Account("thay nha", "1234", "thay nguyen hoang anh", true);
    //    SessionFactory factory = HibernateUtil.getSessionFactory();
    //    Session session = factory.getCurrentSession();
    //    
    //
    //    
    //        try {
    //        session.beginTransaction();
    //        session.save(acc);
    //        session.getTransaction().commit();
    //    }
    //
    //    Catch() {
    //        if(session.getTransaction().isActive()){
    //            session.getTransaction().rollback();
    //        }
    //        
    //    }
    //    }
    
    
}

